package com.supcon.tinyportal.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import com.supcon.tinyportal.portlet.PortletState;

public class TagUtil {


    public static int reload(PageContext pageContext,
                             String target,
                             boolean redirect) throws JspException {
        HttpServletRequest request = (HttpServletRequest) (pageContext.getRequest());
        HttpServletResponse response = (HttpServletResponse) (pageContext.getResponse());

        Object event = request.getAttribute(Constants.PORTLET_EVENT);
        if (event == null) return  TagSupport.EVAL_PAGE;

        String reloadPath = null;

        if (target != null) {
            reloadPath = target;

        } else if (!RequestUtil.isAjax(request)) {
            String parentPage = RequestUtil.getPortletInfo(request).getPageAddress();
            reloadPath = parentPage != null && parentPage.length() > 0 ?
                         parentPage :
                         request.getRequestURL().toString();
        } else {
            return  TagSupport.EVAL_PAGE;
        }

        try {
            if (redirect) {
                response.sendRedirect(response.encodeRedirectURL(reloadPath));
                response.flushBuffer();
            } else {
                String contextPath = request.getContextPath();
                int pos = reloadPath.indexOf(contextPath) + contextPath.length();
                String forwardPath = reloadPath.substring(pos);
                RequestDispatcher requestDispatcher =
                        pageContext.getServletContext().getRequestDispatcher(forwardPath);
                if (requestDispatcher == null) {
                    response.sendError(
                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Request dispatcher cannot be instantiated");
                } else {
                    requestDispatcher.forward(request, response);
                }
            }
        } catch (IOException e) {
            throw new JspException(e);
        } catch (ServletException e) {
            throw new JspException(e);
        }
        return TagSupport.SKIP_PAGE;
    }

    @SuppressWarnings("unchecked")
	public static PortletState getPortletState(PageContext pageContext, String portletId) {
        HttpServletRequest request = (HttpServletRequest) (pageContext.getRequest());
        PortletState state = null;
        Map statusBeans = RequestUtil.getPortletInfo(request).getStateMap();
        state = (PortletState) statusBeans.get(portletId);
        if (state == null) {
        	state = new PortletState();
            statusBeans.put(portletId, state);
        }
        return state;
    }

}
